package com.smfx.yxt.controller.teacher;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.LateCoachType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.common.enums.Week;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.LateCoachRegister;
import com.smfx.yxt.reqVo.LateCoachQueryVO;
import com.smfx.yxt.service.ILateCoachService;
import com.smfx.yxt.utils.DateUtil;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.LATE_COACH)
@Controller
public class LateCoachController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(LateCoachController.class);
	
	@Autowired
	private ILateCoachService lateCoachService;
	
	@RequestMapping(value = {"", "/"})
	public String index() {
		return "lateCoach/index";
	}
	
	/**
	 * 辅导登记
	 * @return
	 */
	@RequestMapping("register/{coachType}")
	public String register(@PathVariable Integer coachType, Model model) {
		String coachTypeText = LateCoachType.getValue(coachType);
		if(StringUtils.isBlank(coachTypeText)) {
			return null;
		}
		model.addAttribute("coachType", coachType);
		model.addAttribute("coachTypeText", coachTypeText);
		int day = DateUtil.getWeekDay(new Date());
		if(day == 0) day = 7;
		model.addAttribute("week", day);
		model.addAttribute("weekText", Week.getValue(day));
		if(LateCoachType.LESSON.getKey() == coachType) {
			return "lateCoach/eightReg";
		}
		return "lateCoach/register";
	}
	
	/**
	 * 辅导统计
	 * @return
	 */
	@RequestMapping("total")
	public String total() {
		return "lateCoach/total";
	}
	
	@RequestMapping("getLateCoachPlan")
	@ResponseBody Object getLateCoachPlan(LateCoachQueryVO vo) {
		return lateCoachService.selectTodayLateCoachList(vo);
	}
	
	@RequestMapping("getTodayNoComeList")
	@ResponseBody Object getTodayNoComeList(LateCoachQueryVO vo) {
		List<LateCoachRegister> list = lateCoachService.selectTodayNoComeList(vo);
		return list;
	}
	
	@RequestMapping("getRegTotalList")
	@ResponseBody Object getRegTotalList(LateCoachQueryVO vo) {
		return lateCoachService.selectRegTotalList(vo);
	}
	
	@RequestMapping("addReg")
	@ResponseBody Object addReg(LateCoachRegister record) {
		try {
			record.setTermId(getCurrentTerm().getId());
			return lateCoachService.addLateCoachRegister(record);
		} catch (Exception e) {
			logger.error("--addReg error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("cancelReg")
	@ResponseBody Object cancelReg(Integer id) {
		try {
			LateCoachRegister record = new LateCoachRegister();
			record.setId(id);
			record.setIsValid(0);
			record.setUpdateTime(new Date());
			lateCoachService.updateLateCoachRegister(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--cancelReg error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
